package com.zpz.framework.zpzoauth;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;
/**
 * @author zhangpanzhi
 * @time 2019-07-09
 * @description 未经本人允许请勿随便改动，尊重劳动
 * */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name ="enabled" ,prefix = "swagger",havingValue = "true",matchIfMissing = false)
public class ZpzOauthSwagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(setHeaderToken());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("用户中心--API")
                .description("描述")
                .contact(new Contact("张盼枝", "", "zhangpanzhi@zccninfo.com"))
                .version("1.0")
                .build();
    }

    private List<Parameter> setHeaderToken() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("Authorization").description("值为：token_type+空格+access_token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        ParameterBuilder liencePar = new ParameterBuilder();
        liencePar.name("license").description("必填,格式为: clientId:apicode:ucode 进行base64,ucode在无登录状态下值为：anyone").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(liencePar.build());
        ParameterBuilder signPar = new ParameterBuilder();
        signPar.name("X-Ca-Proxy-Signature").description("签名（非必填，如若该接口要求入参签名则必填）").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(signPar.build());

        //临时添加头部参数
//        ParameterBuilder userCodeePar = new ParameterBuilder();
//        userCodeePar.name("clientId").description("clientId").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
//        pars.add(userCodeePar.build());
        return pars;
    }

}
